﻿using HtmlAgilityPack;
using NewsCollection.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NewsCollection.Common.Service;
using NewsCollection.Data;
using System.Windows.Forms;
namespace NewsCollection.Common.Service
{
    public class Load
    {
        private ListView lv;
        public Load(ListView lv)
        {
            this.lv = lv;
        }
        ProcessDb pdb = new ProcessDb();

        public delegate void AddCallback(NewsItemModel item);
        public void AddItemToListView(NewsItemModel item)
        {
            if (lv.InvokeRequired)
            {
                lv.BeginInvoke(new AddCallback(AddItemToListView),
                    (object)item);
            }
            else
            {





                ListViewItem lvi = new ListViewItem(item.Title.ToCutString());
                lvi.SubItems.Add(item.Url);
                lvi.SubItems.Add(item.CategoryModel.Name + "");

                lvi.SubItems.Add(item.Hot);
                lv.Items.Add(lvi);



            }
            //treeView1.Refresh();

        }
        private List<CategoryModel> LoadCategory(HtmlAgilityPack.HtmlNode item, string url, string Cate, string urlGoc, CategoryModel parentCategory, string detailNameSubCate, string nameClassA
            , string nameSubClassA)
        {



            //pro.Link = names.Attributes[0].Value;
            // Console.WriteLine(names.Attributes[0].Value);
            // duyet danh sach

            List<CategoryModel> listCateModel = new List<CategoryModel>();
            //  var webPro = new HtmlWeb();
            //  var proDoc=  webPro.Load(node.InnerHtml);
            HtmlAgilityPack.HtmlDocument proDoc = new HtmlAgilityPack.HtmlDocument();

            proDoc.LoadHtml(item.InnerHtml);
            //  var names = cateDoc.DocumentNode.SelectSingleNode("//legend//a");
            HtmlNodeCollection list = null;
            if (Cate == "")
            {
                list = proDoc.DocumentNode.SelectNodes(detailNameSubCate + nameSubClassA);
            }
            else
                list = proDoc.DocumentNode.SelectNodes(nameClassA);

            if (list != null)

                for (int i = 0; i < list.Count(); i++)
                {
                    var simpleMenu = list[i];
                    if (simpleMenu != null)
                    {
                        // lay ra link
                        Boolean flag = false;
                        CategoryModel m = new CategoryModel();

                        m.sId = simpleMenu.InnerText.convertToUnSign3().ToCutString();
                        m.Name = simpleMenu.InnerText;
                        m.ParentCategoryModel = parentCategory;

                        for (int j = 0; j < simpleMenu.Attributes.Count(); j++)
                        {
                            if (simpleMenu.Attributes[j].Name.Equals("href"))
                            {
                                m.Url = urlGoc + simpleMenu.Attributes[j].Value;
                                if (m.sId == "")

                                    m.sId = urlGoc + simpleMenu.Attributes[j].Value;
                                if (Cate != "")
                                    flag = true;
                            }





                        }
                        if (flag)
                        {
                            listCateModel.Add(m);
                            break;
                        }
                        else
                            if (flag == false)
                                listCateModel.Add(m);


                        //  WriteFile(names.Attributes[0].);



                    }
                }
            else
            {
                CategoryModel m = new CategoryModel();

                m.ParentCategoryModel = parentCategory;

                for (int j = 0; j < item.Attributes.Count(); j++)
                {
                    if (item.Attributes[j].Name.Equals("onclick"))
                    {
                        m.Url = urlGoc + item.Attributes[j].Value.Replace("window.location='", "").Replace("'", "").Trim();
                        string[] s = item.Attributes[j].Value.Split('/');
                        m.sId = s[s.Count() - 1].Replace(".html", "");
                        m.Name = s[s.Count() - 1].Replace(".html", "");
                        break;
                    }
                }
                listCateModel.Add(m);
            }

            return listCateModel;


        }
        //detalNameSubCate de lay chi tiet cua subcate la the nao 
        public List<CategoryModel> LoadCatesWithSub(string url, string Cate, string subCate, string urlGoc, int idManufactory, string detailNameSubCate, string nameClassA, string nameSubClassA,
            string positionCateWhenEqual, Boolean loadParentSubCate)
        {

            List<CategoryModel> listCate = new List<CategoryModel>();
            List<CategoryModel> listSubCate = new List<CategoryModel>();
            var web = new HtmlWeb();
            Random r = new Random();
            int time = r.Next(0, 10000);

            System.Threading.Thread.Sleep(time);
            var doc = web.Load(url);
            //  doc.Load(new StringReader(s));

            {

                if (doc.DocumentNode != null)
                {

                    // HtmlAgilityPack.HtmlNode names = doc.DocumentNode.SelectSingleNode("//product_img//a");
                    // lay the div product
                    HtmlNodeCollection list = null;

                    // chi lay 1 cate

                    list = doc.DocumentNode.SelectNodes(Cate);

                    var listSub = doc.DocumentNode.SelectNodes(subCate);
                    if (list != null)
                    {


                        // duyet danh sach
                        if (positionCateWhenEqual == null)
                        {
                            for (int i = 0; i < list.Count(); i++)
                            {
                                if (list[i] != null)
                                {






                                    var item = LoadCategory(list[i], url, Cate, urlGoc, null, "", nameClassA, "");
                                    for (int j = 0; j < item.Count(); j++)
                                    {
                                        if (item[j].sId != null)
                                            listCate.Add(item[j]);
                                    }


                                    if (listSub != null)
                                    {

                                        listSubCate.AddRange(LoadCategory(listSub[i], url, "", urlGoc, item[0], detailNameSubCate, "", nameSubClassA));


                                    }




                                }


                            }
                        }
                        else // neu co position khi trung cate khong lay dc 
                        {
                            var item = LoadCategory(list[Convert.ToInt32(positionCateWhenEqual.Trim())], url, Cate, urlGoc, null, "", nameClassA, "");
                            for (int j = 0; j < item.Count(); j++)
                            {
                                if (item[j].sId != null)
                                    listCate.Add(item[j]);
                            }


                            if (listSub != null)
                            {

                                listSubCate.AddRange(LoadCategory(listSub[0], url, "", urlGoc, item[0], detailNameSubCate, "", nameSubClassA));


                            }

                        }

                    }














                }
            }
            for (int i = 0; i < listCate.Count(); i++)
            {
                var item = listCate[i];
                if (item.sId != null)
                {
                    var condition = pdb.CheckCategoryDb(listCate[i].sId.Trim());


                    Category cate = new Category();

                    if (condition == null)
                    {

                        cate.sId = item.sId.Trim();
                        cate.Name = item.Name.Trim();

                        cate.CreatedDate = DateTime.Today;
                        cate.Description = item.Name.Trim();

                        var aItem = pdb.SaveCategory(cate);
                        item.Id = aItem.Id;
                        listCate[i].Id = aItem.Id;

                    }
                    else
                    {
                        item.Id = condition.Id;
                        listCate[i].Id = condition.Id;
                    }

                    for (int j = 0; j < listSubCate.Count(); j++)
                    {
                        var subItem = listSubCate[j];
                        if (subItem.sId != null)
                        {
                            Category subCategory = new Category();

                            var subCondition = pdb.CheckCategoryDb(subItem.sId.Trim());
                            if (subCondition == null && subItem.ParentCategoryModel == item)
                            {
                                subCategory.sId = subItem.sId.Trim();
                                subCategory.Name = subItem.Name.Trim();

                                subCategory.CreatedDate = DateTime.Today;
                                subCategory.Description = subItem.Name.Trim();
                                subCategory.ParentCategory = pdb.SearchCategory(item.Id);
                                var aSubItem = pdb.SaveCategory(subCategory);
                                subItem.Id = aSubItem.Id;
                                listSubCate[j].Id = aSubItem.Id;
                            }
                            else if (subCondition != null)
                            {
                                subItem.Id = subCondition.Id;
                                listSubCate[j].Id = subCondition.Id;
                            }
                            if (pdb.CheckManuCateMapping(idManufactory, subItem.Id) == null)
                            {
                                Manu_Cate_Mapping mcm = new Manu_Cate_Mapping();
                                mcm.Category = pdb.SearchCategory(subItem.Id);

                                mcm.Manufactory = pdb.SearchManufactory(idManufactory);
                                if (mcm.Category != null && mcm.Manufactory != null)
                                    pdb.SaveManuCateMapping(mcm);

                            }
                        }

                    }
                    if (pdb.CheckManuCateMapping(idManufactory, item.Id) == null)
                    {
                        Manu_Cate_Mapping mcm = new Manu_Cate_Mapping();
                        mcm.Category = pdb.SearchCategory(item.Id);

                        mcm.Manufactory = pdb.SearchManufactory(idManufactory);
                        if (mcm.Category != null && mcm.Manufactory != null)
                            pdb.SaveManuCateMapping(mcm);


                    }
                }



            }
            //them cate vao neu la load noi dung trang Cate
            if (loadParentSubCate == true)
            {
                listSubCate.AddRange(listCate);
            }
            //  listCate.AddRange(listSubCate);

            return listSubCate;
        }
        public List<CategoryModel> LoadCatesNoSub(string url, string Cate, string urlGoc, int idManufactory, string nameClassA)
        {

            List<CategoryModel> listCate = new List<CategoryModel>();

            var web = new HtmlWeb();
            Random r = new Random();
            int time = r.Next(0, 10000);

            System.Threading.Thread.Sleep(time);
            var doc = web.Load(url);
            //  doc.Load(new StringReader(s));

            {

                if (doc.DocumentNode != null)
                {

                    // HtmlAgilityPack.HtmlNode names = doc.DocumentNode.SelectSingleNode("//product_img//a");
                    // lay the div product
                    var list = doc.DocumentNode.SelectNodes(Cate);



                    if (list != null)
                    {


                        // duyet danh sach
                        for (int i = 0; i < list.Count(); i++)
                        {
                            if (list[i] != null)
                            {
                                var item = LoadCategory(list[i], url, Cate, urlGoc, null, "", nameClassA, "");
                                for (int j = 0; j < item.Count(); j++)
                                {
                                    if (item[j].sId != null)
                                        listCate.Add(item[j]);
                                }


                            }



                        }
                    }






                }
            }
            for (int i = 0; i < listCate.Count(); i++)
            {
                var item = listCate[i];
                if (item.sId != null)
                {
                    var condition = pdb.CheckCategoryDb(listCate[i].sId.Trim());


                    Category cate = new Category();

                    if (condition == null && item.Name.ToCutString() != "")
                    {

                        cate.sId = item.sId.Trim();
                        cate.Name = item.Name.Trim();

                        cate.CreatedDate = DateTime.Today;
                        cate.Description = item.Name.Trim();

                        var aItem = pdb.SaveCategory(cate);
                        item.Id = aItem.Id;

                        listCate[i].Id = aItem.Id;

                    }
                    else if (condition != null)
                    {
                        item.Id = condition.Id;
                        listCate[i].Id = condition.Id;
                    }

                    if (pdb.CheckManuCateMapping(idManufactory, item.Id) == null)
                    {
                        Manu_Cate_Mapping mcm = new Manu_Cate_Mapping();
                        mcm.Category = pdb.SearchCategory(item.Id);

                        mcm.Manufactory = pdb.SearchManufactory(idManufactory);
                        if (mcm.Category != null && mcm.Manufactory != null)
                            pdb.SaveManuCateMapping(mcm);

                    }
                }



            }

            return listCate;
        }
        //public List<Page> LoadPage(CategoryModel category, string Cate, string urlGoc)
        //{
        //    List<Page> listPage = new List<Page>();

        //    var web = new HtmlWeb();
        //    Random r = new Random();
        //    int time = r.Next(0, 10000);

        //    System.Threading.Thread.Sleep(time);
        //    var doc = web.Load(category.Url);
        //    //  doc.Load(new StringReader(s));

        //    {

        //        if (doc.DocumentNode != null)
        //        {


        //            var list = doc.DocumentNode.SelectNodes(Cate);


        //            if (list != null)
        //                for (int i = 0; i < list.Count(); i++)
        //                {
        //                    Page np = new Page();
        //                    string name = list[i].InnerText.Trim();

        //                    np.Name = name;
        //                    np.Category = category;
        //                    for (int j = 0; j < list[i].Attributes.Count(); j++)
        //                    {
        //                        if (list[i].Attributes[j].Name.Equals("href"))
        //                        {
        //                            np.Url = urlGoc + list[i].Attributes[j].Value;
        //                            listPage.Add(np);

        //                        }

        //                    }

        //                    if (i == 0)
        //                    {
        //                        np.Name = 0 + "";
        //                        np.Category = category;
        //                        np.Url = category.Url;
        //                        listPage.Add(np);
        //                    }


        //                }
        //        }

        //        return listPage;
        //    }
        //}

        //public List<Page> LoadPageWithPage(string url, string name, Boolean flag, string urlGoc, CategoryModel category)
        //{


        //    var web = new HtmlWeb();
        //    var doc = web.Load(url);
        //    //  doc.Load(new StringReader(s));
        //    List<Page> list = new List<Page>();
        //    Page np = new Page();

        //    if (doc.DocumentNode != null)
        //    {


        //        var item = doc.DocumentNode.SelectSingleNode(name);
        //        string urlItem = "";
        //        for (int j = 0; j < item.Attributes.Count(); j++)
        //        {
        //            if (item.Attributes[j].Name.Equals("href"))
        //            {
        //                urlItem = urlGoc + item.Attributes[j].Value;


        //            }
        //        }


        //        np.Category = category;
        //        np.Url = urlItem;

        //        list.Add(np);
        //        if (flag == false)
        //        {
        //            list.AddRange(LoadPageWithPage(urlItem, name, flag, urlGoc, category));
        //        }


        //    }

        //    return list;
        //}



        // de qui lai load trang va list homenews cung 1 luc', do moi lan chi hien 1 page, dateItem neu nhu ko co ngay o trang home se lay tu trang chi tiet
        // ap cho 1 trang có 1 trang sau 
        public int RecursionPageAndNews(CategoryModel category, string url, string namePage, string nameHomeNews
          , string title, string shortDetail, string urlNews, string urlGoc, string date, int manuId, string detailNameShort, string dateItem, string fullDescription, string nameTagFirstImg, string folder)
        {
            var web = new HtmlWeb();
            Random r = new Random();
            int time = r.Next(0, 10000);

            System.Threading.Thread.Sleep(time);
            var doc = web.Load(url);
            int dem = 0;

            if (doc.DocumentNode != null)
            {









                List<NewsItemModel> ListDetail = new List<NewsItemModel>();

                ListDetail = LoadUpdateNewHomeNews(category, nameHomeNews, title, shortDetail, urlNews, urlGoc);



                for (int k = 0; k < ListDetail.Count(); k++)
                {

                    var item = LoadNews(ListDetail[k], fullDescription, detailNameShort, manuId, 0, nameTagFirstImg, urlGoc,false);

                    if (item != null)
                    {
                        dem++;

                        AddItemToListView(item);
                        pdb.SaveImgNormal(folder, item);

                    }

                    //if (dem == 30)
                    //    break;
                }







            }
            return dem;
        }
        // lay tin trong ngay
        public List<NewsItemModel> LoadUpdateNewHomeNews(CategoryModel PageOneCategory, string Cate
      , string title, string shortDetail, string tabNameUrlNews, string urlGoc)
        {

            List<NewsItemModel> listNewsDetails = new List<NewsItemModel>();

            var web = new HtmlWeb();
            Random r = new Random();
            int time = r.Next(0, 10000);

            System.Threading.Thread.Sleep(time);
            var doc = web.Load(PageOneCategory.Url);
            //    var doc = web.Load("http://afamily.vn/dep.chn");



            if (doc.DocumentNode != null)
            {

                // HtmlAgilityPack.HtmlNode names = doc.DocumentNode.SelectSingleNode("//product_img//a");
                // lay the div product
                var list = doc.DocumentNode.SelectNodes(Cate);

                if (list != null)
                {
                    //pro.Link = names.Attributes[0].Value;
                    // Console.WriteLine(names.Attributes[0].Value);
                    // duyet danh sach
                    for (int i = 0; i < list.Count(); i++)
                    {

                        //  var webPro = new HtmlWeb();
                        //  var proDoc=  webPro.Load(node.InnerHtml);
                        HtmlAgilityPack.HtmlDocument proDoc = new HtmlAgilityPack.HtmlDocument();

                        proDoc.LoadHtml(list[i].InnerHtml);
                        //  var names = cateDoc.DocumentNode.SelectSingleNode("//legend//a");

                        //var urlImg = proDoc.DocumentNode.SelectSingleNode("//a//img");


                        //var urlImg2 = proDoc.DocumentNode.SelectSingleNode("//div");
                        //var urlImg3 = proDoc.DocumentNode.SelectSingleNode("//img");
                        NewsItemModel nd = new NewsItemModel();

                        if (proDoc.DocumentNode.SelectSingleNode(title) != null)
                        {
                            var url = proDoc.DocumentNode.SelectSingleNode(tabNameUrlNews);

                            for (int j = 0; j < url.Attributes.Count(); j++)
                            {

                                if (url.Attributes[j].Name.Equals("href"))
                                {
                                    string sUrl = url.Attributes[j].Value;
                                    if (!sUrl.Contains("http://"))
                                    {
                                        nd.Url = urlGoc + proDoc.DocumentNode.SelectSingleNode(tabNameUrlNews).Attributes[j].Value;
                                    }
                                    else
                                    {
                                        nd.Url = proDoc.DocumentNode.SelectSingleNode(tabNameUrlNews).Attributes[j].Value;
                                    }
                                }
                            }
                            if (pdb.SearchNews(nd.Url) == null)
                            {

                                if (shortDetail != "")
                                    nd.ShortDescription = proDoc.DocumentNode.SelectSingleNode(shortDetail).InnerText;
                                nd.Title = proDoc.DocumentNode.SelectSingleNode(title).InnerText.FixErrorText();

                                //  nd.idCategory = PageOneCategory.Id;
                                nd.CategoryModel = PageOneCategory;
                                listNewsDetails.Add(nd);

                            }

                            else
                            {
                                break;
                            }
                        }


                        //if (dateNews.Date == DateTime.Today && proDoc.DocumentNode.SelectSingleNode(title) !=null)








                        //else if (dateNews.Date < DateTime.Today)
                        //{
                        //    update.flag = true;
                        //    break;
                        //}

                    }
                }
            }



            return listNewsDetails;
        }
        // lay noi dung  nhap vao : NewsModel co url , Cate: the chua noi dung tin , shortDes: mô tả, manuId: mã website, intLoadNews, lay tu noi dung the thu may trong truong` hop
        //có nhiều thẻ trùng nhau không lấy theo tên được, date: neu trang ko co date o ngoai trang home thì sẽ vào từng trang để lấy ngày ra và so sánh
       private string urlImage (HtmlNode item,string urlGoc)
       {
           string urlImage = "";
                                    for (int j = 0; j < item.Attributes.Count(); j++)
                                    {
                                        if (item.Attributes[j].Name.Equals("src"))
                                        {
                                            string img = item.Attributes[j].Value;

                                            if (!img.Contains("http://"))
                                            {
                                                //   nd.Thumb = (urlGoc + img).getImageFromUrl();
                                                urlImage = urlGoc + img;
                                            }
                                            else
                                            {
                                                //  nd.Thumb = img.getImageFromUrl();
                                                urlImage = img;
                                            }
                                          
                                         
                                        }
                                      
                                    }
           return urlImage;
       }
        private void getFirstImageAndSecondImage(HtmlNodeCollection listRelativeImg, News nItem,string urlGoc)
        {
        
            string urlFirstImage = "";
            string urlSecondImage="";
          
            int index = 0;
            if (listRelativeImg != null)
            {
                //hinh 1
                for (int i = 0; i < listRelativeImg.Count(); i++)
                {


                    if (listRelativeImg[i] != null)
                    {
                        urlFirstImage = urlImage(listRelativeImg[i], urlGoc);
                        byte[] imageData1 = Extensions.DownloadData(urlFirstImage);
                        if (Extensions.CheckImageWithWidth(imageData1))
                        {
                            nItem.UrlFirstImage = urlFirstImage;
                            nItem.FirstImage = imageData1;
                            index = i;
                            break;
                        }

                    }


                }

                //hinh 2
                for (int i = index+1; i < listRelativeImg.Count(); i++)
                {


                    if (listRelativeImg[i] != null)
                    {
                        urlSecondImage = urlImage(listRelativeImg[i], urlGoc);
                        byte[] imageData = Extensions.DownloadData(urlSecondImage);
                        if (Extensions.CheckImageWithWidth(imageData))
                        {

                        
                            nItem.SecondImage = imageData;
                          
                            break;
                        }

                    }


                }
              

            }
                    

                      
                   

        }
        public NewsItemModel LoadNews(NewsItemModel news, string Cate, string ShortDes, int manuId, int intLoadNews, string nameTagFirstImg, string urlGoc
            ,Boolean isFeatured)
        {


            var web = new HtmlWeb();
            Random r = new Random();
            int time = r.Next(0, 10000);

            System.Threading.Thread.Sleep(time);
            var doc = web.Load(news.Url);
            //  doc.Load(new StringReader(s));



            if (doc.DocumentNode != null)
            {



                //var test=  db.News.Where(m=>m.Url.Equals(news.Url)).ToList();



                //smartBase.CheckUrlInDb(news.Url)== false &&
                //
                if (doc.DocumentNode.SelectSingleNode(Cate) != null)
                {

                    // kiem tra ngay


                    // neu la ngay hien tai , và dateItem là khac rong

                    //       News newsVisit = pdb.UrlNewsVitedToday(news.Url);


                    News n = new News();
                    n.CreatedDate = DateTime.Now;

                    //   n.Thumb = news.Url.Trim().getImageFromUrl();
                    if (ShortDes == "")
                        n.ShortDescription = news.ShortDescription.FixErrorText().Trim();
                    else
                        if (doc.DocumentNode.SelectSingleNode(ShortDes) != null)
                            n.ShortDescription = doc.DocumentNode.SelectSingleNode(ShortDes).InnerText.FixErrorText().Trim();
                    // n.ParentId = news.ParentId;

                    n.Title = news.Title.ToCutString();

                    n.Category = pdb.SearchCategory(news.CategoryModel.Id);
                    //   n.Featured = Featured;
                    n.Manufactory = pdb.SearchManufactory(manuId);


                   
                  
                   
                        var listDes = doc.DocumentNode.SelectNodes(Cate);
                        //     nItem.FullDescription = doc.DocumentNode.SelectSingleNode(Cate).InnerHtml.Trim();
                        for (int i = intLoadNews; i < listDes.Count(); i++)
                        {

                            n.FullDescription += listDes[i].InnerHtml.Trim();
                        }
                        //lay ra list co image
                        var listRelativeImg = doc.DocumentNode.SelectNodes(nameTagFirstImg);

                        getFirstImageAndSecondImage(listRelativeImg, n, urlGoc);
                        //thumb image
                        if (n.UrlFirstImage != null)
                        {
                            n.UrlImage = news.UrlImage;
                            n.CreatedDate = DateTime.Now;

                        
                            n.sId = news.Title.convertToUnSign3().ToCutString();
                            n.Url = news.Url;
                            n.IsFeatured = isFeatured;
                            var newNews = pdb.SaveNews(n);
                            news.UrlFirstImage = newNews.UrlFirstImage;
                            news.Id = newNews.Id;
                            news.FirstImage = newNews.FirstImage;
                            news.SecondImage = newNews.SecondImage;
                            return news;
                        }

                    










                }
            }
            return null;
        }





    

    }
























}

